package org.edu.modules.sxjy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.edu.common.api.vo.Result;
import org.edu.modules.sxjy.entity.VeClassTeacher;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeCompanyUser;
import org.edu.modules.sxjy.entity.VeLog;
import org.edu.modules.sxjy.entity.VePractice;
import org.edu.modules.sxjy.entity.VeStudent;
import org.edu.modules.sxjy.feign.VeCommonClient;
import org.edu.modules.sxjy.mapper.VeLogMapper;
import org.edu.modules.sxjy.service.IVeClassTeacharService;
import org.edu.modules.sxjy.service.IVeCompanyUserService;
import org.edu.modules.sxjy.service.IVeLogService;
import org.edu.modules.sxjy.service.IVePracticeService;
import org.edu.modules.sxjy.service.IVeStudentService;
import org.edu.modules.sxjy.service.IVeTeacharService;
import org.edu.modules.sxjy.utils.DateUtils;
import org.edu.modules.sxjy.vo.VeLogInfoVo;
import org.edu.modules.sxjy.vo.VeLogShowCpPostVo;
import org.edu.modules.sxjy.vo.VeLogVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VeLogServiceImpl
        extends ServiceImpl<VeLogMapper, VeLog>
        implements IVeLogService
{
    @Autowired
    private IVeLogService veLogService;
    @Autowired
    private VeLogMapper veLogMapper;
    @Autowired
    private IVeStudentService veStudentService;
    @Autowired
    private IVePracticeService vePracticeService;
    @Autowired
    private VeCommonClient veCommonClient;
    @Autowired
    private IVeTeacharService veTeacharService;
    @Autowired
    private IVeClassTeacharService veClassTeacharService;
    @Autowired
    private IVeCompanyUserService veCompanyUserService;

    public List<VeLogVo> selectPraLog(VeLogVo veLogVo, Integer startLine, Integer pageSize, VeCommon veCommon)
    {
        List<VeLogVo> veLogVoList = this.veLogMapper.selectPraLog(veLogVo, startLine, pageSize, veCommon);
        VeCompanyUser veCompanyUser;
        if (veCommon.getUserType().contains("0"))
        {
            veLogVoList = this.veLogMapper.selectPraLog(veLogVo, startLine, pageSize, veCommon);
        }
        else if (veCommon.getUserType().equals("1"))
        {
            List<Integer> stuBjIdList = new ArrayList();

            List<VeClassTeacher> veClassTeacherList = this.veClassTeacharService.getBanjiIdByTeacherUserId(veCommon.getUserId());
            for (VeClassTeacher veClassTeacher : veClassTeacherList) {
                if ((veClassTeacher != null) && (!"".equals(veClassTeacher))) {
                    stuBjIdList.add(Integer.valueOf(veClassTeacher.getBjId()));
                }
            }
            veCommon.setStuBjId(stuBjIdList);
            if ((stuBjIdList != null) && (stuBjIdList.size() > 0)) {
                veLogVoList = this.veLogMapper.selectPraLog(veLogVo, startLine, pageSize, veCommon);
            } else {
                veLogVoList = new ArrayList();
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
            Map stuMap = (Map)stuResult;
            if (stuMap != null)
            {
                veCommon.setStuId(Integer.parseInt(stuMap.get("id").toString()));
                veLogVoList = this.veLogMapper.selectPraLog(veLogVo, startLine, pageSize, veCommon);
            }
        }
        else if ((veCommon.getUserType().equals("4")) || (veCommon.getUserType().equals("3")))
        {
            veCompanyUser = this.veCompanyUserService.getCpInfoByUserId(veCommon.getUserId());
            veCommon.setCpId(veCompanyUser.getCpId());

            veLogVoList = this.veLogMapper.selectPraLog(veLogVo, startLine, pageSize, veCommon);
        }
        if ((veLogVoList != null) && (veLogVoList.size() > 0)) {
            for (VeLogVo veLogVo1 : veLogVoList)
            {
                veLogVo1.setEndTime(DateUtils.getDateToString(Long.parseLong(veLogVo1.getEndTime())));
                veLogVo1.setStartTime(DateUtils.getDateToString(Long.parseLong(veLogVo1.getStartTime())));
                veLogVo1.setCreateTime(DateUtils.getDateToString(Long.parseLong(veLogVo1.getCreateTime())));
                if (veLogVo1.getType().equals("day")) {
                    veLogVo1.setType("日记");
                } else if (veLogVo1.getType().equals("week")) {
                    veLogVo1.setType("周记");
                } else if (veLogVo1.getType().equals("month")) {
                    veLogVo1.setType("月记");
                }
            }
        }
        return veLogVoList;
    }

    public int getSumPage(VeLogVo veLogVo, VeCommon veCommon)
    {
        int pageNum = 0;
        if (veCommon.getUserType().contains("0"))
        {
            pageNum = this.veLogMapper.getSumPage(veLogVo, veCommon);
        }
        else if (veCommon.getUserType().equals("1"))
        {
            List<Integer> stuBjIdList = new ArrayList();

            List<VeClassTeacher> veClassTeacherList = this.veClassTeacharService.getBanjiIdByTeacherUserId(veCommon.getUserId());
            for (VeClassTeacher veClassTeacher : veClassTeacherList) {
                if ((veClassTeacher != null) && (!"".equals(veClassTeacher))) {
                    stuBjIdList.add(Integer.valueOf(veClassTeacher.getBjId()));
                }
            }
            veCommon.setStuBjId(stuBjIdList);
            if ((stuBjIdList != null) && (stuBjIdList.size() > 0)) {
                pageNum = this.veLogMapper.getSumPage(veLogVo, veCommon);
            } else {
                pageNum = 0;
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
            Map stuMap = (Map)stuResult;
            if (stuMap != null)
            {
                veCommon.setStuId(Integer.parseInt(stuMap.get("id").toString()));
                pageNum = this.veLogMapper.getSumPage(veLogVo, veCommon);
            }
        }
        else if ((veCommon.getUserType().equals("4")) || (veCommon.getUserType().equals("3")))
        {
            VeCompanyUser veCompanyUser = this.veCompanyUserService.getCpInfoByUserId(veCommon.getUserId());
            veCommon.setCpId(veCompanyUser.getCpId());

            pageNum = this.veLogMapper.getSumPage(veLogVo, veCommon);
        }
        return pageNum;
    }

    public VeLogInfoVo getOneInfo(int id)
    {
        VeLog veLog = (VeLog)this.veLogService.getById(Integer.valueOf(id));

        VeLogInfoVo veLogInfoVo = new VeLogInfoVo();

        BeanUtils.copyProperties(veLog, veLogInfoVo);


        veLogInfoVo.setStartTime(DateUtils.getDateToString(veLog.getStartTime()));


        Object stuResult = this.veCommonClient.getStudentById("09d5e1e7f9b049008eee645c783a1d67", veLog.getStuUserId()).getResult();
        Map stuMap = (Map)stuResult;
        String s = JSON.toJSONString(stuMap);
        VeStudent veStudent = (VeStudent)JSON.parseObject(s, VeStudent.class);
        if ((veStudent != null) && (!"".equals(veStudent)))
        {
            veLogInfoVo.setXh(veStudent.getXh());

            veLogInfoVo.setXm(veStudent.getXm());
        }
        String cpName = this.veLogMapper.getCpNameByStuId(veLog.getStuUserId());
        if (cpName != null) {
            veLogInfoVo.setCp(cpName);
        }
        String postName = this.veLogMapper.getPostNameByStuId(veLog.getStuUserId());
        if ((postName != null) && (!"".equals(postName))) {
            veLogInfoVo.setPost(postName);
        }
        if (veLogInfoVo.getType().equals("day")) {
            veLogInfoVo.setType("日记");
        } else if (veLogInfoVo.getType().equals("week")) {
            veLogInfoVo.setType("周记");
        } else if (veLogInfoVo.getType().equals("month")) {
            veLogInfoVo.setType("月记");
        }
        Result<?> linkway = this.veCommonClient.queryDictDataByModelCode("09d5e1e7f9b049008eee645c783a1d67", "LINKWAY", veLog.getLinkWay());
        Object result = linkway.getResult();
        List<Object> linkwayObject = (List)result;
        for (Object o : linkwayObject)
        {
            Map map = (Map)o;
            if ((map.get("modelCode").equals("LINKWAY")) &&
                    (map.get("code").equals(veLog.getLinkWay()))) {
                veLogInfoVo.setLinkWay((String)map.get("title"));
            }
        }
        return veLogInfoVo;
    }

    public void saves(VeLogInfoVo veLogInfoVo)
    {
        VeLog veLog = new VeLog();
        if ((veLogInfoVo.getType() != null) && (!"".equals(veLogInfoVo.getType()))) {
            veLog.setType(veLogInfoVo.getType());
        }
        veLog.setName(veLogInfoVo.getName());


        veLog.setContent(veLogInfoVo.getContent());

        veLog.setSummary(veLogInfoVo.getSummary());

        Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veLogInfoVo.getUserId()).getResult();
        Map stuMap = (Map)stuResult;
        String s = JSON.toJSONString(stuMap);
        VeStudent veStudent = (VeStudent)JSON.parseObject(s, VeStudent.class);

        veLog.setStuUserId(veStudent.getId());

        veLog.setSteacherContent(veLogInfoVo.getSteacherContent());

        veLog.setCteacherContent(veLogInfoVo.getCteacherContent());

        Result<?> linkway = this.veCommonClient.queryDictDataByModelCode("09d5e1e7f9b049008eee645c783a1d67", "LINKWAY", veLogInfoVo.getLinkWay());
        List<Object> linkwayResult = (List)linkway.getResult();
        for (Object o : linkwayResult)
        {
            Map map = (Map)o;
            if ((map.get("modelCode").equals("LINKWAY")) &&
                    (map.get("code").equals(veLogInfoVo.getLinkWay()))) {
                veLog.setLinkWay((String)map.get("code"));
            }
        }
        veLog.setCreateTime(DateUtils.getStringToDate(veLogInfoVo.getStartTime()));




        veLog.setStartTime(DateUtils.getStringToDate(veLogInfoVo.getStartTime()));
        if (veLogInfoVo.getType().equals("day"))
        {
            long stringToDate = DateUtils.getStringToDate(veLogInfoVo.getStartTime());
            long l = stringToDate + 86400000L;
            veLog.setEndTime(l);
        }
        else if (veLogInfoVo.getType().equals("week"))
        {
            long stringToDate = DateUtils.getStringToDate(veLogInfoVo.getStartTime());
            long l = stringToDate + 604800000L;
            veLog.setEndTime(l);
        }
        else if (veLogInfoVo.getType().equals("month"))
        {
            long stringToDate = DateUtils.getStringToDate(veLogInfoVo.getStartTime());
            long l = stringToDate + 2592000000L;
            veLog.setEndTime(l);
        }
        veLog.setTerminalId(1);


        this.veLogService.save(veLog);
    }

    public VeLogShowCpPostVo showCpPostName(String userId)
    {
        VeLogShowCpPostVo veLogShowCpPostVo = new VeLogShowCpPostVo();

        Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", userId).getResult();
        Map stuMap = (Map)stuResult;
        if (stuMap != null)
        {
            VePractice vePractice = this.veLogMapper.getStuPracticeInfoByStuId(Integer.parseInt(stuMap.get("id").toString()));
            if ((vePractice != null) && (!"".equals(vePractice)))
            {
                veLogShowCpPostVo.setCp(vePractice.getCpName());

                veLogShowCpPostVo.setCpId(vePractice.getCpId());

                veLogShowCpPostVo.setPost(vePractice.getPostName());

                veLogShowCpPostVo.setPostId(vePractice.getPostId());

                veLogShowCpPostVo.setXm(vePractice.getXm());

                veLogShowCpPostVo.setXh(vePractice.getXh());
            }
        }
        return veLogShowCpPostVo;
    }

    public VeLogInfoVo editEcho(int id)
    {
        VeLogInfoVo veLogInfoVo = new VeLogInfoVo();
        VeLog veLog = (VeLog)this.veLogService.getById(Integer.valueOf(id));

        List<VePractice> vePracticeList = this.vePracticeService.list();


        VeStudent veStudent = (VeStudent)this.veStudentService.getById(Integer.valueOf(veLog.getStuUserId()));
        if ((veStudent != null) && (!"".equals(veStudent)))
        {
            veLogInfoVo.setXh(veStudent.getXh());

            veLogInfoVo.setXm(veStudent.getXm());
        }
        veLogInfoVo.setId(veLog.getId());


        String cpName = this.veLogMapper.getCpNameByStuId(veLog.getStuUserId());
        if ((cpName != null) && (!"".equals(cpName))) {
            veLogInfoVo.setCp(cpName);
        }
        String postName = this.veLogMapper.getPostNameByStuId(veLog.getStuUserId());
        if ((postName != null) && (!"".equals(postName))) {
            veLogInfoVo.setPost(postName);
        }
        veLogInfoVo.setType(veLog.getType());

        veLogInfoVo.setName(veLog.getName());

        veLogInfoVo.setStartTime(DateUtils.getDateToString(veLog.getStartTime()));

        veLogInfoVo.setContent(veLog.getContent());

        veLogInfoVo.setSummary(veLog.getSummary());

        veLogInfoVo.setSteacherContent(veLog.getSteacherContent());

        veLogInfoVo.setCteacherContent(veLog.getCteacherContent());

        Result<?> linkway = this.veCommonClient.queryDictDataByModelCode("09d5e1e7f9b049008eee645c783a1d67", "LINKWAY", veLog.getLinkWay());
        Object result = linkway.getResult();
        List<Object> linkwayObject = (List)result;
        for (Object o : linkwayObject)
        {
            Map map = (Map)o;
            if ((map.get("modelCode").equals("LINKWAY")) &&
                    (map.get("code").equals(veLog.getLinkWay()))) {
                veLogInfoVo.setLinkWay((String)map.get("title"));
            }
        }
        return veLogInfoVo;
    }

    public void edit(VeLogInfoVo veLogInfoVo)
    {
        VeLog veLog = (VeLog)this.veLogService.getById(Integer.valueOf(veLogInfoVo.getId()));


        veLog.setId(veLogInfoVo.getId());










        veLog.setType(veLogInfoVo.getType());


        veLog.setName(veLogInfoVo.getName());


        veLog.setStartTime(DateUtils.getStringToDate(veLogInfoVo.getStartTime()));
        if (veLogInfoVo.getType().equals("day"))
        {
            long stringToDate = DateUtils.getStringToDate(veLogInfoVo.getStartTime());
            long l = stringToDate + 86400000L;
            veLog.setEndTime(l);
        }
        else if (veLogInfoVo.getType().equals("week"))
        {
            long stringToDate = DateUtils.getStringToDate(veLogInfoVo.getStartTime());
            long l = stringToDate + 604800000L;
            veLog.setEndTime(l);
        }
        else if (veLogInfoVo.getType().equals("month"))
        {
            long stringToDate = DateUtils.getStringToDate(veLogInfoVo.getStartTime());
            long l = stringToDate + 2592000000L;
            veLog.setEndTime(l);
        }
        veLog.setContent(veLogInfoVo.getContent());

        veLog.setSummary(veLogInfoVo.getSummary());

        veLog.setSteacherContent(veLogInfoVo.getSteacherContent());

        veLog.setCteacherContent(veLogInfoVo.getCteacherContent());
        if ((veLogInfoVo.getLinkWay() != null) && (!"".equals(veLogInfoVo.getLinkWay()))) {
            veLog.setLinkWay(veLogInfoVo.getLinkWay());
        }
        veLog.setCreateTime(DateUtils.getStringToDate(veLogInfoVo.getStartTime()));


        this.veLogService.updateById(veLog);
    }
}
